package utilities.print.menu.execute.userPortal;

import utilities.JDBC.Database;
import utilities.print.menu.Menu;
import utilities.print.menu.MenuExecute;
import utilities.print.menu.execute.Verify;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import static utilities.print.menu.Menu.line;

public class PrintList implements MenuExecute {

    /**
     * 启动菜单功能类方法
     *
     * @throws Exception 可能产生的异常
     */
    @Override
    public void execute() throws Exception {
        System.out.println("查看所有账号");
        line();
        printDatabaseUserInfo();

        Verify.pressKey("输入任意内容退出");
    }

    /**
     * 打印所有在数据库中的账号信息
     * @throws SQLException 可能产生的sql错误
     */
    public static void printDatabaseUserInfo() throws SQLException {
        Database<Object> database = new Database<>();
        System.out.println("账号编号\t\t账号名\t\t创建人的名字");
        Menu.line();
        database.toList(
                database.executeSelect(
                        connection -> connection.prepareStatement("select U.userId,U.userName,E.`name` from " + "userinfo as U left outer join employeeinfo as E on U.userId = E.userId order by U.userId"
                        )
                ),
                set -> {
                    System.out.printf("%s\t\t%s\t\t%s\n",
                            set.getString("userId"),set.getString("userName"),set.getString("name")
                    );
                    return null;
                }
        );
        Menu.line();
        database.close();
    }
}
